//
// Created by hps on 24-4-21.
//

#include "AddNoteDialog.h"

namespace hps {
AddNoteDialog::AddNoteDialog(AddNoteData *noteData, const std::string& bookPath)
    : m_noteData(noteData), m_bookPath(bookPath) {
    m_layout = new QVBoxLayout();
    m_bookNameInputer = new hps::LineInputer("笔记本");
    m_noteNameInputer = new hps::LineInputer("笔记");
    m_opButtons = new hps::ChooseOrCancelButton();
    init();
    setUi();
    rspFunc();
}
void AddNoteDialog::init() {
    m_layout->setContentsMargins(50, 60, 50, 20);
    m_layout->setSpacing(30);
    m_bookNameInputer->setLineEditReadOnly();

    std::string bookName = hps::getNameFromDir(m_bookPath);
    m_bookNameInputer ->setText(bookName.c_str());
}
void AddNoteDialog::setUi() {
    this ->setWindowTitle("新建笔记");
    this ->setLayout(m_layout);
    m_layout ->insertWidget(0, m_bookNameInputer);
    m_layout ->insertWidget(1, m_noteNameInputer);
    m_layout ->insertWidget(2, m_opButtons);
}

void AddNoteDialog::clickedOk() {
    if (m_noteData == nullptr) {
        QMessageBox::critical(this, "Error", "resData is null!");
        std::cerr << "resData is null" << __FILE__ << ":" << __LINE__ << std::endl;
    }
    m_noteData->_bookName = m_bookNameInputer->getLineEditComment();
    m_noteData->_noteName = m_noteNameInputer->getLineEditComment();
    if (m_noteData->_noteName.empty()) {
        QMessageBox::critical(this, "Error", "请输入笔记名!");
        return ;
    }
    accept();
}

void AddNoteDialog::clickedCansel() {
    reject();
}
void AddNoteDialog::rspFunc() {
    QObject::connect(m_opButtons -> getOKButton(), &QPushButton::clicked, this, &AddNoteDialog::clickedOk);
    QObject::connect(m_opButtons -> getCanselButton(), &QPushButton::clicked, this, &AddNoteDialog::clickedCansel);
}
} // hps